﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.label
 * 唯一标识：76b4ce63-b59f-43c5-be2a-555535bda0e5
 * 文件名：Link
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/26 13:18:36
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// Link 链接
/// </summary>
public sealed class Link : ControlBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Link()
    {
    }

    public override string Id { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        Param par = new Param();

        if (Type != TextType.Default) par.Add($"type=\"{Type.GetDescription()}\"");

        if (HasUnderline) par.Add($"underline=\"false\"");

        if (!string.IsNullOrEmpty(Href))
        {
            par.Add($"href=\"{Href}\" ");

            par.Add($"target=\"{Target.GetDescription()}\"");
        }

        sb.AppendLine($"<el-link {par} >");
        sb.AppendLine(Title);
        sb.AppendLine($"</el-link>");

        return sb.ToString();
    }

    /// <summary>
    /// 类型
    /// </summary>
    internal TextType Type { get; set; }

    /// <summary>
    /// 是否有下划线
    /// </summary>
    internal bool HasUnderline { get; set; }

    /// <summary>
    /// 原生 href 属性
    /// </summary>
    internal string Href { get; set; }

    /// <summary>
    /// 跳转方式
    /// </summary>
    internal LinkTargetType Target { get; set; }

    public override string Title { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建link控件
    /// </summary>
    /// <param name="content"></param>
    /// <param name="type"></param>
    /// <param name="hasUnderline"></param>
    /// <param name="href"></param>
    /// <param name="targetType"></param>
    /// <returns></returns>
    public static Link CreateLink(string content, TextType type = TextType.Default, bool hasUnderline = false, string href = "", LinkTargetType targetType = LinkTargetType.Self)
    {
        Link link = new Link()
        {
            Id = GenerateId(),
            Title = content,
            Type = type,
            HasUnderline = hasUnderline,
            Href = href,
            Target = targetType
        };

        return link;
    }

    /// <summary>
    /// 创建link控件
    /// </summary>
    /// <param name="children"></param>
    /// <param name="type"></param>
    /// <param name="hasUnderline"></param>
    /// <param name="href"></param>
    /// <param name="targetType"></param>
    /// <returns></returns>
    public static Link CreateLink(Action<List<ControlBase>> children, TextType type = TextType.Default, bool hasUnderline = false, string href = "", LinkTargetType targetType = LinkTargetType.Self)
    {
        List<ControlBase> controls = new List<ControlBase>();

        children?.Invoke(controls);

        return CreateLink(string.Join("\r\n", controls.Select(s => s.ConverToHtml())), type, hasUnderline, href, targetType);
    }
}

/// <summary>
/// 链接跳转方式
/// </summary>
public enum LinkTargetType
{
    /// <summary>
    /// 当前页面调整
    /// </summary>
    [Description("_self")]
    Self,

    /// <summary>
    /// 新页面
    /// </summary>
    [Description("_blank")]
    Blank
}